	
//销售机会登记
jQuery('#btnReg').on("click",function()
	{
		var url=window.rootPath+'/ui/chance/chanceRegister';
		window.location = url;
	});
var serverData = {};
serverData.key ='';
var state=$('input:radio[name="isReceived"]:checked').val();
serverData.state = state;
var	serverTable;
jQuery(document).ready(function () {

	   serverTable=  jQuery('#dataTable').DataTable({
        "serverSide": true,
        "ordering": false,
        "ajax": {
            "dataType": "json",
            "type": "get",
            "cache": false,
            "url": window.rootPath+'/ui/chance/getpage',
            "data": function (d) {
                return $.extend({}, d, serverData);
            }
        },
        columns: [
            {
            data: null,
            defaultContent: ""
            },{
		    data:null,
		    render: function(data, type, full, meta){
		    	var operate ='<input type="checkbox" class="checkchild">';
		    	var id=data.id;
		    	return operate;
						
				}
           },
            {
            	 data: null,
				 render: function(data, type, full, meta){
					var dOut;
                    var isSubmit=data.isSubmit;
                    var isSucceed = data.isSucceed;
                    if(isSucceed != null){
                		dOut ='<p><span class="badge" style="background-color:red;font-family:arial;color:bule">已关闭-'+data.isSucceed+'</span></p>';
                	}else{
                    if(isSubmit == "1")
                   {
                    	dOut = '<p><span class="badge" style="background-color:green;font-family:arial;color:bule">已提交</span></p>';
                   }
                    else{
                    	dOut = '<p><span class="badge">未提交</span></p>';
                    }
                    
					}
                    return dOut;
				 }
            },
            {
            	data: 'name',
				render:function(data,type,full,meta){
					if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
					}
					//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
				}
            },
            {
            	data: 'chancePriority'
            },
            {
            	data: 'business'
            },
            {
                data: 'source'
            },
            {
                data: 'sourceUnit',
				render:function(data,type,full,meta){
					if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'user.empLogin.userName'
            },
            {
                data: 'sSourceArea'
            },
            {
                data: 'sourceOfferInfo',
				render:function(data,type,full,meta){
					if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'projectState',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'deviceState',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'sourcePower'
            },
            {
                data: 'orderAmount'
            },
            {
                data: 'orderUnit',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
					}
				}
            },
            {
                data: 'orderPerson'
            },
            {
                data: 'orderUnitAddr',
                render:function(data,type,full,meta){
                	if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
					}
				}
            },
            {
            	data: 'createTime',
            	render: function (data, type, full, meta) {
                    if (data)
                        return moment(data).format('YYYY/MM/DD');
                    else
                        return '';
                }
            },
            {
            	data: 'memo',
            	render:function(data,type,full,meta){
            		if(data == null || data == undefined || data == ""){
						return data;
					}else{
						return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
					}
				}
            }
            ],
        "drawCallback": function ( oSettings ) {             
            var k = 0;
            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

            for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
            {
                $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
                k++;
            }
  
            serverTable.off('click','tr');
            serverTable.on( 'click', 'tr', function (event) {
                event=event||window.event;
                var target=event.target||event.scrElement;
                var data = serverTable.row( this ).data();

                return;
            } );
        }
    });

    //修改
    jQuery('.editBtn').on('click', function() {
    	var selects=getSelect("#dataTable",1);
  	   if(selects.length>1){
  		   $.springLemonDialog({
  		         content: '只能编辑一条记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }
  	   else if(selects.length==0){
  		   $.springLemonDialog({
  		         content: '请选择要编辑的记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }else if(selects.length==1){
  		 var id=selects[0].id;
  		 if(selects[0].isSubmit =="1" || selects[0].closeReason != null){
  			$.springLemonDialog({
 		         content: '已提交或已关闭信息不可修改！！',// 确认文本
 		         clickButton: function (sender, modal, index) {// 按钮事件处理
 		             $(this).closeDialog(modal);
 		         }
 		   });  
  		 }
  		 else{
  			window.location = window.rootPath + "/ui/chance/edit"+"?chanceId="+id+"&bus="+selects[0].business; 
  		 }
  		 
  	   }
	});
  //提交信息
    jQuery('.submitBtn').on('click', function() {
    	var selects=getSelect("#dataTable",1);
    	var url = window.rootPath + "/ui/chance/submitChance";
 	   submitSelect(selects,url,serverTable) 
    });
    //查看详情
    jQuery('.detailBtn').on('click', function() {
    	var selects=getSelect("#dataTable",1);
  	   if(selects.length>1){
  		   $.springLemonDialog({
  		         content: '只能操作一条记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }
  	   else if(selects.length==0){
  		   $.springLemonDialog({
  		         content: '请选择要操作的记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }else if(selects.length==1){
  		 var id=selects[0].id;
  		window.location = window.rootPath + "/ui/chance/detail"+"?chanceId="+id;  
  	   }
	});
    //删除信息
    jQuery('.deleBtn').on('click', function() {
    	var selects=getSelect("#dataTable",1);
    	var url = window.rootPath + "/ui/chance/deleteChance";
 	   delChanceSelect(selects,url,serverTable) 
    });
  //获取选中状态
	 $(function(){
		  $(":radio").click(function(){
			  serverData.key = jQuery("#keyword").val();
			  var state=$('input:radio[name="isReceived"]:checked').val();
			  serverData.state = state;
			  serverTable.draw();
		  });
	});
    //查询
    jQuery('.searchBtn').on('click', function() {
		serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	});
	// 撤销查询
	jQuery('.resetBtn').on("click", function() {
		jQuery('#keyword').val('');
		serverData.key = '';
		serverTable.draw();
	});
	   ///////列表中的全选
    $("#checkAll").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
	});
    
    function submitSelect(selects,url,tableName){
    	if(selects.length==0){
    	       $.springLemonDialog({
    	         content: '请选择要提交的信息',// 确认文本
    	         clickButton: function (sender, modal, index) {// 按钮事件处理
    	             $(this).closeDialog(modal);
    	         }
    	       }); 
    	   }else{
    		   var ids=selects[0].id;
    		   var operate ="0";
    		   for(var i=0;i<selects.length;i++){
    			   if(selects[i].isSubmit =="1" || selects[i].closeReason != null){
    				   operate ="1";
    			   }
    			   
    		   }
    		   if(operate == "1"){
    	  			$.springLemonDialog({
    	 		         content: '已提交或已关闭信息不可操作！！',// 确认文本
    	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
    	 		             $(this).closeDialog(modal);
    	 		         }
    	 		   });  
    	  		 }
    		   else{
    			   
        		   $.springLemonDialog({
        		         content: '确认提交'+selects.length+'条信息？',// 确认文本
        		         clickButton: function (sender, modal, index) {// 按钮事件处理
        		        	 if (index == 0) {
        		        		 for(var i=0;i<selects.length;i++){
        		        			 
        		        			 var d=
        		        			 {
        		        				"receiveSuperior":selects[i].sourceSuperior,
        		        				"saleChance":{"id":selects[i].id}
        		        			 }
        		        			 jQuery.ajax({
        									url : encodeURI(url),
        									type : 'post',
        									data : JSON.stringify(d),
        									dataType : 'json',
        									contentType : 'application/json;charset=utf-8',
        									success : function() {
        										serverTable.draw(false);
        									}
        								}); 
        		        			 
        		        		   }
        		        		 	
        		        	 }
        		             $(this).closeDialog(modal);
        		         }
        		       });  
    		   }
    		     
    	   }
    	
    }
    function delChanceSelect(selects,url,tableName){
    	if(selects.length==0){
    	       $.springLemonDialog({
    	         content: '请选择删除的记录',// 确认文本
    	         clickButton: function (sender, modal, index) {// 按钮事件处理
    	             $(this).closeDialog(modal);
    	         }
    	       }); 
    	   }else{
    		   var ids=selects[0].id;
    		   var operate ="0";
    		   for(var i=0;i<selects.length;i++){
    			   if(selects[i].isSubmit =="1" || selects[i].closeReason != null){
    				   operate ="1";
    			   }
    			   
    		   }
    		   if(operate == "1"){
    			   $.springLemonDialog({
  	 		         content: '已提交或已关闭信息不可删除！！',// 确认文本
  	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
  	 		             $(this).closeDialog(modal);
  	 		         }
  	 		   }); 
    		   }
    		   else{
    			   for(var i=1;i<selects.length;i++){
        			   ids += ',' + selects[i].id;  
        		   }
        		   var api=url+"?ids="+ids;
        		   $.springLemonDialog({
        		         content: '确认删除'+selects.length+'条记录？',// 确认文本
        		         clickButton: function (sender, modal, index) {// 按钮事件处理
        		        	 if (index == 0) {
        		        		 jQuery.ajax({
        		        			    url:encodeURI(api),
        		        	            cache:false,
        		        	            type :"get",
        		        	            dataType:"json",
        		        	            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
        		        	            success:function(data){
        		        	            	
        		        	            	tableName.draw(false); 
        		        	            }
        		        	        });	
        		        	 }
        		             $(this).closeDialog(modal);
        		         }
        		       });
    		   }
    		     
    	   }
    	
    }
    //查询
    jQuery('.searchBtn').on('click', function() {
		serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	});
	// 撤销查询
	jQuery('.cancelBtn').on("click", function() {
		jQuery('#keyword').val('');
		serverData.key = '';
		serverTable.draw();
	});
});